package cn.itcast.springmvc.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import cn.itcast.springmvc.pojo.User;
import cn.itcast.springmvc.pojo.UserVO;

@RequestMapping("hello")
@Controller
public class Hello2Controller {

	@RequestMapping(value = "show1")
	public ModelAndView test1(){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("hello");
		mv.addObject("msg", "springmvc的第一个注解程序！");
		return mv;
	}
	
	@RequestMapping(value = "s*/show2")
	public ModelAndView test2(){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("hello");
		mv.addObject("msg", "ant风格的映射：*");
		return mv;
	}
	
	@RequestMapping(value = "**/show2")
	public ModelAndView test3(){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("hello");
		mv.addObject("msg", "ant风格的映射：**");
		return mv;
	}
	
	@RequestMapping(value = "show4/{name}/{id}")
	public ModelAndView test4(@PathVariable("name")String name,@PathVariable("id")Long id){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("hello");
		mv.addObject("msg", "rest风格的映射：name=" + name + ",id=" + id);
		return mv;
	}
	@RequestMapping(value="show5",method=RequestMethod.POST)
	public ModelAndView test5(){
		ModelAndView mView=new ModelAndView("hello");
		mView.addObject("msg","springmvc请求路径之限定请求方法:POST");
		return mView;
	}
	@RequestMapping(value="show6",method={RequestMethod.POST , RequestMethod.GET})
	public ModelAndView test6(){
		ModelAndView mView=new ModelAndView("hello");
		mView.addObject("msg","springmvc请求路径之限定请求方法：POST/GET");
		return mView;
	}
	@RequestMapping(value="show8", params="id")
	public ModelAndView test8(){
		ModelAndView mv = new ModelAndView("hello");
		mv.addObject("msg", "springmvc的映射之限定请求参数，id");
		return mv;
	}
	
	@RequestMapping(value="show9", params="!id")
	public ModelAndView test9(){
		ModelAndView mv = new ModelAndView("hello");
		mv.addObject("msg", "springmvc的映射之限定请求参数，!id");
		return mv;
	}
	
	@RequestMapping(value="show10", params="id=1")
	public ModelAndView test10(){
		ModelAndView mv = new ModelAndView("hello");
		mv.addObject("msg", "springmvc的映射之限定请求参数，id=1");
		return mv;
	}
	
	@RequestMapping(value="show11", params="id!=1")
	public ModelAndView test11(){
		ModelAndView mv = new ModelAndView("hello");
		mv.addObject("msg", "springmvc的映射之限定请求参数，id!=1");
		return mv;
	}
	
	@RequestMapping(value="show12", params={"id","name"})
	public ModelAndView test12(){
		ModelAndView mv = new ModelAndView("hello");
		mv.addObject("msg", "springmvc的映射之限定请求参数，id,name");
		return mv;
	}
	@GetMapping(value="show113")
	public ModelAndView test113(){
		ModelAndView mv = new ModelAndView("hello");
		mv.addObject("msg", "GetMapping");
		return mv;
	}
	@PostMapping(value="show114")
	public ModelAndView test114(){
		ModelAndView mv = new ModelAndView("hello");
		mv.addObject("msg", "PostMapping");
		return mv;
	}
	@PutMapping(value="show115")
	public ModelAndView test115(){
		ModelAndView mv = new ModelAndView("hello");
		mv.addObject("msg", "PutMapping");
		return mv;
	}
	@DeleteMapping(value="show116")
	public ModelAndView test116(){
		ModelAndView mv = new ModelAndView("hello");
		mv.addObject("msg", "DeleteMapping");
		return mv;
	}
	@RequestMapping(value="show17")
	public ModelAndView test17(HttpServletRequest request, HttpServletResponse response, HttpSession session){
		StringBuffer sb = new StringBuffer();
		sb.append("request:" + request.toString() + "<br />");
		sb.append("response:" + response.toString() + "<br />");
		sb.append("session:" + session.toString() + "<br />");
		ModelAndView mv = new ModelAndView("hello");
		mv.addObject("msg", sb.toString());
		return mv;
	}
	@RequestMapping(value="show18")
	public String test18(Model model){
		model.addAttribute("msg", "springmvc特有的内置对象");
		return "hello";
	}
	@RequestMapping(value="show19/{name}/{id}")
	public String test19(Model model, @PathVariable("name")String name, @PathVariable("id")Long id){
		model.addAttribute("msg", "springmvc接收占位符参数：name=" + name + ",id=" + id);
		return "hello";
	}
	
	@RequestMapping(value="show20")
	public String test20(Model model, @RequestParam("id")Long id, @RequestParam(value="name", required=true, defaultValue="aaa")String name){
		model.addAttribute("msg", "springmvc接收占位符参数：name=" + name + ",id=" + id);
		return "hello";
	}
	
	@RequestMapping(value="show21")
	public String test21(Model model, HttpServletRequest request){
		Cookie[] cookies = request.getCookies();
		if(cookies!=null){
			for (Cookie cookie : cookies) {
				if("JSESSIONID".equals(cookie.getName())){
					model.addAttribute("msg", "JSESSIONID:" + cookie.getValue());
				}
			}
		}
		return "hello";
	}
	
	@RequestMapping(value="show22")
	public String test22(Model model, @CookieValue("JSESSIONID")String sessionId){
		model.addAttribute("msg", "JSESSIONID:" + sessionId);
		return "hello";
	}
	
	@RequestMapping(value="show23")
	@ResponseStatus(value=HttpStatus.OK)
	public void test23(@RequestParam("name")String name,
			@RequestParam("age")Integer age,
			@RequestParam("isMarry")Boolean isMarry,
			@RequestParam("income")Float income,
			@RequestParam("interests")String[] interests
			){
		StringBuffer sb = new StringBuffer();
		sb.append("name:" + name + "\n");
		sb.append("age:" + age + "\n");
		sb.append("isMarry:" + isMarry + "\n");
		sb.append("income:" + income + "\n");
		sb.append("interests:[");
		for (String interest : interests) {
			sb.append(interest + " ");
		}
		sb.append("]");
		System.out.println(sb.toString());
	}
	
	@RequestMapping(value="show24")
	public String test24(Model model, User user, @RequestParam("name")String name){
		model.addAttribute("msg", user.toString() + "    " + name);
		return "hello";
	}
	
	@RequestMapping(value="show25")
	public String test25(Model model, UserVO userVO){
		model.addAttribute("msg", userVO.getUsers().toString());
		return "hello";
	}
	
	@RequestMapping(value="show26")
	public String test26(Model model, @RequestParam("ids")List<Long> ids){
		model.addAttribute("msg", ids);
		return "hello";
	}
	
	@RequestMapping(value="show27")
	public String test27(Model model){
		List<User> userList = new ArrayList<>();
		for(int i=0; i<20; i++){
			User user = new User();
			user.setAge(16+i);
			user.setName("大黄");
			user.setUserName("bigYellow");
			user.setId(i+1l);
			userList.add(user);
		}
		model.addAttribute("userList", userList);
		return "users";
	}
	
	@RequestMapping(value="show28")
	@ResponseBody
	public List<User> test28(){
		List<User> userList = new ArrayList<>();
		for(int i=0; i<20; i++){
			User user = new User();
			user.setAge(16+i);
			user.setName("大黄");
			user.setUserName("bigYellow");
			user.setId(i+1l);
			userList.add(user);
		}
		return userList;
	}
	
	@RequestMapping(value="show29")
	public String test29(@RequestBody() User user, Model model){
		model.addAttribute("msg", user.toString());
		return "hello";
	}
	
	@RequestMapping(value="show30")
	public String test30(@RequestBody() String user, Model model){
		model.addAttribute("msg", user.toString());
		return "hello";
	}
	
	@RequestMapping(value="show31")
	public String test31(@RequestParam("file")MultipartFile file) throws IllegalStateException, IOException{
		if(file!=null){
			file.transferTo(new File("E:\\tmp\\" +file.getOriginalFilename()));
		}
		return "redirect:/success.html";
	}
	
	@RequestMapping(value="show32")
	public String test32(){
		return "forward:show34.do?id=111&type=forward";
	}
	@RequestMapping(value="show33")
	public String test33(){
		return "redirect:show34.do?id=222&type=redirect";
	}
	@RequestMapping(value="show34")
	public String test34(Model model, @RequestParam("id")Long id, @RequestParam("type")String type){
		model.addAttribute("msg", "forward？redirect？type=" + type + ",id=" + id);
		return "hello";
	}
}
